// linedepth2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//int _tmain(int argc, _TCHAR* argv[])
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdlib.h>

using namespace std;

#define delta 0.01
#define INF 10000 

//#define _SLOW_

double min(double, double); 
double min(double, double, double); 
double gamaheck();


double check2()
{
	double a,b,c,d,t;
	t=0.28;
	double mini=1000;


	for (a=1-3*t;a<=t;a+=delta)
		for(b=1-3*t;b<=t;b+=delta)
			
			for(c=0;c<=(4*t-1);c+=delta)
				for(d=0;d<=(4*t-1-c);d+=delta)
				{
					if(a+b+c+d<1-2*t)
						continue;
					if( a*(b+c)+b*d+min(a,b,c)<mini )
						mini = a*(b+c)+b*d+min(a,b,c);
				
				}


		return mini;

}


//double gama[4], gama[3], beta[1][2], gama[2], beta[1][3], beta[1][4], alpha[1], gama[1], beta[2][3], beta[2][4], alpha[2], beta[3][4], alpha[3], alpha[4];
//double mgama[4], mgama[3], mbeta[1][2], mgama[2], mbeta[1][3], mbeta[1][4], malpha[1], mgama[1], mbeta[2][3], mbeta[2][4], malpha[2], mbeta[3][4], malpha[3], malpha[4];

double alpha[5], gama[5];double beta[5][5];
double malpha[5], mgama[5];double mbeta[5][5];
int orgdir, dir;
double part1,part2,__a,__b;
ofstream oo;

double limit = 0.3;

//int main(int argv, char **argc)
int _tmain(int argc, _TCHAR* argv[])
{
	//check2();
	double min = INF; 
	double tmpmin; 
	double q = 0.28;
	//double q = atof(argc[1]); 

	oo.open("D:\\code\\output.txt");

	// oo << "q is: "<< q << endl; 

#ifndef _SLOW_
	//keeping gamma's zero for now to improve speed
	gama[4]=0; gama[3]=0; gama[2]=0;gama[1]=0;
#else

	for (gama[4] =0; gama[4] <= limit; gama[4] += delta) 
		for (gama[3]=0; gama[3] <= gama[4]; gama[3] += delta) 
			for (gama[2]=0; gama[2] <= gama[4]; gama[2] += delta) 
				for (gama[1]=0; gama[1] <= gama[4]; gama[1] += delta) 
#endif

					for (beta[1][2]=0; beta[1][2] <= limit; beta[1][2] += delta) { 
						for (beta[1][3]=0; beta[1][3] <= limit; beta[1][3] += delta) { 
							for (beta[1][4]=0; beta[1][4] <= limit; beta[1][4] += delta) { 
								alpha[1] = q - (gama[4]+gama[3]+beta[1][2]+gama[2]+beta[1][3]+beta[1][4]); 
								if (alpha[1] > 1 || alpha[1] < -0.001) break; 
								for (beta[2][3]=0; beta[2][3] <= limit; beta[2][3] += delta) { 
									for (beta[2][4]=0; beta[2][4] <= limit; beta[2][4] += delta) { 
										alpha[2] = q - (beta[2][4]+beta[2][3]+gama[1]+gama[4]+gama[3]+beta[1][2]); 
										alpha[3] = (1-q) - (beta[1][3]+alpha[1]+gama[4]+beta[1][2]+beta[2][3]+alpha[2]);  
										alpha[4] = (1-q) - (gama[3]+beta[1][2]+alpha[2]+beta[2][4]+beta[1][4]+alpha[1]); 
										beta[3][4] = q - (gama[4]+gama[2]+beta[1][3]+beta[2][3]+gama[1]+alpha[3]); 
										tmpmin = gamaheck( ); 
										if ( tmpmin < min ) {
											min = tmpmin;
											orgdir=dir;
											__a=part1,__b=part2;
											mgama[4] = gama[4]; mgama[3] = gama[3]; mbeta[1][2] = beta[1][2]; mgama[2] = gama[2]; mbeta[1][3] = beta[1][3] ; mbeta[1][4] = beta[1][4] ; malpha[1] = alpha[1] ; mgama[1] = gama[1] ; mbeta[2][3] = beta[2][3] ; mbeta[2][4] = beta[2][4] ; malpha[2] = alpha[2] ; mbeta[3][4] = beta[3][4] ; malpha[3] = alpha[3] ; malpha[4] = alpha[4] ;
										}
									}
								}
							}
						}
					}

					oo<<"q: "<<q << " depth: 1/" << 1/min<<" in direction ="<<orgdir << endl<<endl; 
					// cout << "pppn: " << mgama[4] << ", ppnp: " << mgama[3] << ", ppnn: " << mbeta[1][2] << ", pnpp: " << mgama[2] << ", pnpn: " << mbeta[1][3] << ", pnnp: " << mbeta[1][4] << ", pnnn: " << malpha[1] << ", nppp: " << mgama[1] << ", nppn: " << mbeta[2][3] << ", npnp: " << mbeta[2][4] << ", npnn: " << malpha[2] << ", nnpp: " << mbeta[3][4] << ", nnpn: " << malpha[3] << ", nnnp: " << malpha[4] << endl;

					oo  << "a_1: " << malpha[1] << ", a_2: " << malpha[2] << ", a_3: " << malpha[3] << ", a_4: " << malpha[4]<<endl<<endl;
					oo<< "b_12: " << mbeta[1][2] << ", b_13: " << mbeta[1][3] << ", b_14: " << mbeta[1][4]<< ", b_23: "<<endl << mbeta[2][3] << ", b_24: " << mbeta[2][4] << ", b_34: " << mbeta[3][4] <<endl<<__a<<" "<<__b<<endl;
					//oo<< "c_1: " << mgama[1] << ", c_2: " << mgama[2] << ", c_3: " << mgama[3] << "c_4: "<< mgama[4] << endl;

					oo.close();
					//cin>>dir;
					return 0;
}


double gamaheck( )
{
	double edges = 0.0;

	if (gama[4] < 0 || gama[3] < 0 || beta[1][2] < 0 || gama[2] < 0 || beta[1][3] < 0 || beta[1][4] < 0 || alpha[1] < 0 || gama[1] < 0 || beta[2][3] < 0 || beta[2][4] < 0 || alpha[2] < 0 || beta[3][4] < 0 || alpha[3] < 0 || alpha[4] < 0 || gama[4] > 1 || gama[3] > 1 || beta[1][2] > 1 || gama[2] > 1 || beta[1][3] > 1 || beta[1][4] > 1 || alpha[1] > 1 || gama[1] > 1 || beta[2][3] > 1 || beta[2][4] > 1 || alpha[2] > 1 || beta[3][4] > 1 || alpha[3] > 1 || alpha[4] > 1)
		return INF; 

	double tmpmin; 
	edges = INF; 

	double a[10];

	//goto case9;
	
	//13. beta[2][3][1]*beta[2][3][1] + alpha[3][1]*alpha[3][2] + beta[3][4][1]*beta[3][4][2]+ alpha[3][1]*gama[3] + alpha[3][2]*(gama[2]+beta[1]3)
	//+alpha[3][2]*alpha[2]

	for (a[0]=0; a[0] <= gama[1]; a[0] += delta) { 
		for (a[1]=0; a[1] <= gama[4]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[2][3]; a[2] += delta) { 				
				
				tmpmin = alpha[2]*alpha[3]+
					alpha[2]*(a[2])+alpha[3]*(beta[2][3]-a[2]);
	
				if (tmpmin < edges) {edges = tmpmin;dir=13;}					
			}
		}
	}


	//14. beta[2][3][1]*beta[2][3][1] + alpha[3][1]*alpha[3][2] + beta[3][4][1]*beta[3][4][2]+ alpha[3][1]*gama[3] + alpha[3][2]*(gama[2]+beta[1]3)
	//+alpha[3][2]*alpha[2]

	for (a[0]=0; a[0] <= gama[1]; a[0] += delta) { 
		for (a[1]=0; a[1] <= alpha[2]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[2][3]; a[2] += delta) { 				
				tmpmin = (a[1])*alpha[3]+alpha[4]*(alpha[2]-a[1])+

					alpha[3]*a[2] + a[1]*(beta[2][3]-a[2]) +
					(alpha[2]-a[1])*beta[2][4];
				if (tmpmin < edges) {edges = tmpmin;dir=14;}
			}
		}
	}

	//15. beta[2][3][1]*beta[2][3][1] + alpha[3][1]*alpha[3][2] + beta[3][4][1]*beta[3][4][2]+ alpha[3][1]*gama[3] + alpha[3][2]*(gama[2]+beta[1]3)
	//+alpha[3][2]*alpha[2]

	for (a[0]=0; a[0] <= gama[1]; a[0] += delta) { 
		for (a[1]=0; a[1] <= alpha[2]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[2][4]; a[2] += delta) {

				tmpmin = (a[1])*alpha[3]+alpha[4]*(alpha[2]-a[1])+
					//removed a[1]*(beta[2][4]-a[2])
					a[1]*beta[2][3] +
					(alpha[2]-a[1])*a[2] +
					(beta[2][4]-a[2])*alpha[4];

				if (tmpmin < edges) {edges = tmpmin;dir=15;}
			}
		}
	}

	// 16 alpha2 gama3

	for (a[0]=0; a[0] <= gama[1]; a[0] += delta) { 
		for (a[1]=0; a[1] <= gama[3]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[2][4]; a[2] += delta) {

				tmpmin = alpha[4]*alpha[2]+

					alpha[2]*(beta[2][4]-a[2])+
					alpha[4]*(a[2]);
				if (tmpmin < edges) {edges = tmpmin;dir=16;}
			}
		}
	}

	// 17 gama1 alpha4

	for (a[0]=0; a[0] <= alpha[4]; a[0] += delta) { 
		for (a[1]=0; a[1] <= gama[3]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[2][4]; a[2] += delta) {

				tmpmin = a[0]*alpha[2]+(alpha[4]-a[0])*alpha[1]+

					alpha[2]*a[2] + (beta[2][4]-a[2])*a[0]+
					(alpha[4]-a[0])*beta[1][4];

				if (tmpmin < edges) {edges = tmpmin;dir=17;}
			}
		}
	}

	// 18 beta24 beta14

	for (a[0]=0; a[0] <= alpha[4]; a[0] += delta) { 
		for (a[1]=0; a[1] <= gama[3]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[1][4]; a[2] += delta) {

				tmpmin = a[0]*alpha[2]+(alpha[4]-a[0])*alpha[1]+

					a[0]*beta[2][4]+(alpha[4]-a[0])*a[2]+(beta[1][4]-a[2])*alpha[1];

				if (tmpmin < edges) {edges = tmpmin;dir=18;}
			}
		}
	}

	// 19 gama3 alpha1...special case

	for (a[0]=0; a[0] <= alpha[4]; a[0] += delta) { 
		for (a[1]=0; a[1] <= alpha[1]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[1][4]; a[2] += delta) {

				tmpmin = a[0]*(alpha[1]-a[1])+a[1]*(alpha[4]-a[0]) + a[0]*alpha[2] + alpha[3]*(alpha[1]-a[1]) +

					a[1]*(beta[1][4]-a[2])+(beta[1][4]-a[2])*a[0]+(alpha[4]-a[0])*a[2]+a[2]*(alpha[1]-a[1]) + alpha[2]*a[2] +
					beta[1][3]*(alpha[1]-a[1]) + beta[2][4]*(a[0]);

				if (tmpmin < edges) {edges = tmpmin;dir=19;part1=a[0];part2=a[1];}
			}
		}
	}


	// 20 alpha4 gama2

	for (a[0]=0; a[0] <= gama[2]; a[0] += delta) { 
		for (a[1]=0; a[1] <= alpha[1]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[1][4]; a[2] += delta) {

				tmpmin = a[1]*alpha[4]+ (alpha[1]-a[1])*alpha[3] +

					beta[1][3]*(alpha[1]-a[1]) + a[1]*(beta[1][4]-a[2]) + a[2]*alpha[4];

				if (tmpmin < edges) {edges = tmpmin;dir=20;part1=a[0];part2=a[1];}
			}
		}
	}

	//21 beta14 beta13
	for (a[0]=0; a[0] <= gama[2]; a[0] += delta) { 
		for (a[1]=0; a[1] <= alpha[1]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[1][3]; a[2] += delta) {

				tmpmin = a[1]*alpha[4]+ (alpha[1]-a[1])*alpha[3] +

					beta[1][4]*a[1] + (alpha[1]-a[1])*(a[2]) + (beta[1][3]-a[2])*alpha[3];

				if (tmpmin < edges) {edges = tmpmin;dir=21;part1=a[0];part2=a[1];}
			}
		}
	}


	//22 alpha1 gama4

	for (a[0]=0; a[0] <= gama[2]; a[0] += delta) { 
		for (a[1]=0; a[1] <= gama[4]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[1][3]; a[2] += delta) {

				tmpmin = alpha[1]*alpha[3] +

					(alpha[1])*(a[2]) + (beta[1][3]-a[2])*alpha[3];

				if (tmpmin < edges) {edges = tmpmin;dir=22;part1=a[0];part2=a[1];}
			}
		}
	}


	//23 gama2 alpha3

	for (a[0]=0; a[0] <= alpha[3]; a[0] += delta) { 
		for (a[1]=0; a[1] <= gama[4]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[1][3]; a[2] += delta) {

				tmpmin = alpha[1]*a[0] +(alpha[3]-a[0])*alpha[2]+

					a[2]*alpha[1] +
				
					(beta[1][3]-a[2])*a[0] + beta[2][3]*(alpha[3]-a[0]);

				if (tmpmin < edges) {edges = tmpmin;dir=23;part1=a[0];part2=a[2];}
			}
		}
	}


	//24 beta13 beta23

	for (a[0]=0; a[0] <= alpha[3]; a[0] += delta) { 
		for (a[1]=0; a[1] <= gama[4]; a[1] += delta) { 
			for (a[2]=0; a[2] <= beta[2][3]; a[2] += delta) {

				tmpmin = alpha[1]*a[0] +(alpha[3]-a[0])*alpha[2]+
					//removed a[0]*(beta[2][3]-a[2]) + 
					a[2]*(alpha[3]-a[0] ) +	
					(beta[2][3]-a[2])*(alpha[2]) + beta[1][3]*a[0] ;

				if (tmpmin < edges) {edges = tmpmin;dir=24;part1=a[0];part2=a[2];}
			}
		}
	}




	return edges;

}

double min(double x, double y)
{
	if (x < y) return x;
	else return y;
}

double min(double x, double y, double z)
{
	if (x <= y && x <= z) return x;
	if (y <= x && y <= z) return y;
	if (z <= x && z <= y) return z;

	return x; 
}




